package com.peng.community.actuator;

import com.peng.community.util.CommunityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;

@Component
@Endpoint(id = "database")
@Slf4j
public class DatabaseEndpoint {

    @Autowired
    private DataSource dataSource;

    @ReadOperation
    public String getConnection() {
        try (
                Connection connection = dataSource.getConnection()
        ) {
            return CommunityUtils.toJsonString(0,"连接获取成功");
        } catch (Exception e) {
            log.error("连接获取失败,{}", e.getCause().toString());
        }
        return null;
    };

}
